package com.fastloan.safe.core.supers;

import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;

/**
 * @author rankai
 *         createTime 2017-11-2017/11/11 18:44
 */
public class PutMultipartResolver extends CommonsMultipartResolver {

    private static final String POST_METHOD = "POST";

    private static final String PUT_METHOD = "PUT";

    @Override
    public boolean isMultipart(HttpServletRequest request) {
        if (POST_METHOD.equalsIgnoreCase(request.getMethod())) {
            return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
        }
        if (PUT_METHOD.equalsIgnoreCase(request.getMethod())) {
            String contentType = request.getContentType();
            if (contentType != null) {
                if (contentType.contains(ServletFileUpload.MULTIPART_FORM_DATA)) {
                    return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
                }
            }
        }
        return false;
    }
}
